﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace HowItWentDown.Enemies
{
    class CageMonster : Enemy
    {
        bool DEBUG = false;
        float runningTime = 0; // keeps track of how much time has passed in b/w attacks
        float attackCooldown = 240; // how much time must pass before another attack
        bool cageThrowing = false; // is the cage currently in motion?
        int attackRange = 100; // how many units away can the monster attack
        Cage cage;
        float life = 240; // object should only stay alive for 240 frames
        float freezeTimer = 360; // how long we make the player stop moving for if they are hit
        float runningFreeze = 0;
        public CageMonster(Game1 doc, float newX, float newY, float newZ)
            : base(doc, newX, newY, newZ, Global.LoadTexture("EnemyTextures\\cageMonster2"))
        {
            cage = new Cage(doc, newX - 10, newY + 10, newZ - 20);
            scale = 0.5f;
        }

        public override void Update()
        {
            if (MyMath.Distance(pos, Global.Player.pos) < attackRange)
            {
                // the shoot timer is increased by the speed of the game
                runningTime += Global.Speed;
                // if the timer is more than 240 (2 seconds, or 240 frames at 60FPS), the monster shoots
                if (runningTime > attackCooldown)
                {
                    runningTime = 0;
                    cageThrowing = true;
                    cage.velocity = Extensions.Normalize(Global.Player.pos - this.pos, 1.6f);
                    cage.targetPos = Global.Player.pos;
                    //var ball = new ShadowBall(doc, x, y, z, 1.5f, Global.Player.pos + new Vector3(0, 20, 0));
                }
            }
            if (this.velocity == Vector3.Zero && cageThrowing) // if we stopped moving that means the cage hit the player
            {
                runningFreeze += Global.Speed; // start the timer
                if (runningFreeze > freezeTimer) // after the specified time, reset the cage
                {
                    cage.pos = new Vector3(this.pos.X - 10, this.pos.Y + 10, this.pos.Z - 20);
                    Global.Player.held = false;
                    cageThrowing = false;
                    runningTime = -35; // this gives the player some time to get away
                }
            }
            
            base.Update();
        }
        public override void remove()
        {
            cage.remove();
            base.remove();
        }
    }
    
    public class Cage : Obj
    {
        float timer;
        float smoketimer;
        float cageSpeed;
        public Vector3 targetPos;
        public Cage(Game1 Doc, float X, float Y, float Z)
            : base(Doc, X, Y, Z, Global.LoadTexture("EnemyTextures\\cageMonster_cage2"), false)
        {
            
            //velocity = Extensions.Normalize(Target - pos, cageSpeed);
            collision = true;
            rect.CenterOrigin();
        }
        public override void Update()
        {
            base.Update();
        }
    }
}
